/*
 *      Created on: Mar 8, 2011
 *      Author: topcoder
 */
#include <Length.h>
#include <LengthUnit.h>

namespace
{
    const LengthUnit lengthUnit[3] = {MILE, YARD, FEET};
    const unsigned int MAX_NUM_OF_LENGTH_UNIT = sizeof(lengthUnit)/sizeof(Unit);
}

Length::Length(const unsigned int amount, const LengthUnit& lengthUnit)
:Quantity(amount, lengthUnit)
{
}

Length Length::operator+(const Length& length)const
{
    return Length(amountInBaseUnit_m+length.amountInBaseUnit_m, LENGTH_BASE_UNIT);
}

void Length::PrintLength(ostream& os)const
{
    unsigned int i = 0,amount = amountInBaseUnit_m;

    for(i = 0; i < MAX_NUM_OF_LENGTH_UNIT; i++) 
    {            
	if(amount >= (lengthUnit[i].getFactor()))
        {
           os<< amount/(lengthUnit[i].getFactor()) << ' ' << lengthUnit[i] << ' ';              
        }
        amount = amount%(lengthUnit[i].getFactor());
    }
}

void Length::PrintInFormat1(ostream& os)const
{   
    if(amountInBaseUnit_m == 0)
    {
        os<< amountInBaseUnit_m << ' ' << unit_m<< ' ';
    }
    else 
    {
        PrintLength(os);
    }
}

void Length::PrintInFormat2(ostream& os)const
{    
    os<< amountInBaseUnit_m << ' ' << LENGTH_BASE_UNIT;
}


